//
//  CDAppleLoginStep.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/4/24.
//

import Combine
struct CDAppleLoginStep: LoginStep {
    struct InputData {
        let userId: String
        let authcode: String
    }

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.AppleLogin.Response, Error> {
        Future { promise in
            let api = AuthAPI.AppleLogin(body: .init(appleUser:input.userId, socialState: "123456789", socialCode: input.authcode))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                case .success(let response):
                    if let response = response.data
                    {
                        Logger.log(.info, message: "苹果登陆成功: \(response)",tag: .login)
                        promise(.success(response))
                    }
                    else{
                        promise(.failure(NSError(domain: "苹果登录", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                case .failure(let error):
                    promise(.failure(error))
                }
            })
        }
        .eraseToAnyPublisher()
    }
}
